<!-- 新增编辑车辆 -->
<template>
  <div>
    <div class="add-vehicle">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="车辆类型"
          prop="truckTypeId"
        >
          <el-select
            v-model="ruleForm.truckTypeId"
            placeholder="请选择车辆类型"
          >
            <el-option
              v-for="ele in carModelsList"
              :key="ele.id"
              :label="ele.name"
              :value="ele.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="车牌号码"
          prop="licensePlate"
        >
          <el-input
            v-model="ruleForm.licensePlate"
            placeholder="请输入车牌号码"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="GPS设备ID"
          prop="deviceGpsId"
        >
          <el-input
            v-model="ruleForm.deviceGpsId"
            placeholder="请输入GPS设备ID"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button @click="resetForm('ruleForm')">取消</el-button>
          <el-button
            type="primary"
            @click="addVehicle(('ruleForm'))"
          >确认</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addVehicle } from '@/api/vehicle'

export default {
  name: 'VehicleAdd',
  props: {
    carModelsList: {
      type: Array
    },
    getVehicleList: {
      type: Function
    },
    getWorkingCarList: {
      type: Function
    }
  },
  data() {
    return {
      ruleForm: {
        truckTypeId: '',
        licensePlate: '',
        deviceGpsId: ''
      },
      rules: {
        truckTypeId: [
          { required: true, message: '车辆类型不能为空', trigger: 'change' }
        ],
        licensePlate: [
          { required: true, message: '车牌号码不能为空', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/, message: '请输入正确格式的车牌号码', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: 'GPS设备ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async addVehicle() {
      const that = this
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          // 新增一条
          const addRes = await addVehicle(that.ruleForm)
          if (addRes.code === 200) {
            that.$message.success(`添加成功`)
            that.getVehicleList()
            that.getWorkingCarList()
            that.$emit('changeDialogVisible', false)
          } else {
            that.$message.error(`${addRes.msg}`)
          }
        }
      })
    },
    resetForm() {
      this.$emit('changeDialogVisible', false)
    }
  }
}

</script>
<style scoped lang="scss">
.btn {
  margin-bottom: 0;

  ::v-deep .el-form-item__content {
    margin-left: 0 !important;
  }
}
::v-deep .el-form-item__label {
  font-weight: 400;
}
::v-deep .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
</style>
